import BaseModel from './.model';
import { Table, Column, DataType, HasMany, BelongsTo, ForeignKey } from 'sequelize-typescript';
import ProductModel from './product.model';

@Table({ tableName: 'categories' })
export default class CategoryModel extends BaseModel {
  /** 类别名称 */
  @Column({ type: DataType.STRING, comment: '类别名称' })
  name!: string;

  /** 排序号 */
  @Column({ type: DataType.INTEGER, comment: '排序号' })
  sortOrder!: number;

  /** 子类别 */
  @HasMany(() => CategoryModel, { foreignKey: 'parentId' })
  children?: CategoryModel[];

  /** 父类别ID */
  @ForeignKey(() => CategoryModel)
  @Column({ type: DataType.UUID, allowNull: true, comment: '父类别ID' })
  parentId?: string;

  /** 该类别下的产品 */
  @HasMany(() => ProductModel)
  products?: ProductModel[];

  /** 父类别 */
  @BelongsTo(() => CategoryModel, { foreignKey: 'parentId' })
  parent?: CategoryModel;
}
